package com.moxiao.yfjh.module.blind.controller.admin.level;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.common.GenNoCommonMethod;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelExcelVO;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelRespVO;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelUpdateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMembershipSystemVo;
import com.moxiao.yfjh.module.blind.controller.admin.mechanism.vo.MemberMechanismExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.privilege.vo.MemberPrivilegeExportReqVO;
import com.moxiao.yfjh.module.blind.convert.level.BlindMemberLevelConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.level.BlindMemberLevelDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.mechanism.MemberMechanismDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.privilege.MemberPrivilegeDO;
import com.moxiao.yfjh.module.blind.service.level.BlindMemberLevelService;
import com.moxiao.yfjh.module.blind.service.mechanism.MemberMechanismService;
import com.moxiao.yfjh.module.blind.service.privilege.MemberPrivilegeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 相亲会员等级")
@RestController
@RequestMapping("/blind/member-level")
@Validated
public class BlindMemberLevelController {

    @Resource
    private BlindMemberLevelService memberLevelService;

    @Resource
    private MemberMechanismService memberMechanismService;

    @Resource
    private MemberPrivilegeService memberPrivilegeService;

    @Resource
    private GenNoCommonMethod genNoCommonMethod;


    @PostMapping("/create")
    @Operation(summary = "创建相亲会员等级")
    @PreAuthorize("@ss.hasPermission('blind:member-level:create')")
    public CommonResult<Long> createMemberLevel(@Valid @RequestBody BlindMemberLevelCreateReqVO createReqVO) {
        genNoCommonMethod.addMemberLevelNo(createReqVO);
        return success(memberLevelService.createMemberLevel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲会员等级")
    @PreAuthorize("@ss.hasPermission('blind:member-level:update')")
    public CommonResult<Boolean> updateMemberLevel(@Valid @RequestBody BlindMemberLevelUpdateReqVO updateReqVO) {
        memberLevelService.updateMemberLevel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲会员等级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:member-level:delete')")
    public CommonResult<Boolean> deleteMemberLevel(@RequestParam("id") Long id) {
        memberLevelService.deleteMemberLevel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲会员等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:member-level:query')")
    public CommonResult<BlindMemberLevelRespVO> getMemberLevel(@RequestParam("id") Long id) {
        BlindMemberLevelDO memberLevel = memberLevelService.getMemberLevel(id);
        return success(BlindMemberLevelConvert.INSTANCE.convert(memberLevel));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲会员等级列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:member-level:query')")
    public CommonResult<List<BlindMemberLevelRespVO>> getMemberLevelList(@RequestParam("ids") Collection<Long> ids) {
        List<BlindMemberLevelDO> list = memberLevelService.getMemberLevelList(ids);
        return success(BlindMemberLevelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲会员等级分页")
    @PreAuthorize("@ss.hasPermission('blind:member-level:query')")
    public CommonResult<PageResult<BlindMemberLevelRespVO>> getMemberLevelPage(@Valid BlindMemberLevelPageReqVO pageVO) {
        PageResult<BlindMemberLevelDO> pageResult = memberLevelService.getMemberLevelPage(pageVO);
        return success(BlindMemberLevelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲会员等级 Excel")
    @PreAuthorize("@ss.hasPermission('blind:member-level:export')")
    @OperateLog(type = EXPORT)
    public void exportMemberLevelExcel(@Valid BlindMemberLevelExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlindMemberLevelDO> list = memberLevelService.getMemberLevelList(exportReqVO);
        // 导出 Excel
        List<BlindMemberLevelExcelVO> datas = BlindMemberLevelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲会员等级.xls", "数据", BlindMemberLevelExcelVO.class, datas);
    }


    @GetMapping("/getUserPrivilege")
    @Operation(summary = "获取会员尊享特权")
    @Parameter(name = "levelNo", description = "会员等级编号")
    public CommonResult<List<BlindMembershipSystemVo>> getUserPrivilege(String levelNo) {

        List<BlindMembershipSystemVo> membershipSystemVos = new ArrayList<>();
        BlindMemberLevelExportReqVO memberLevelExportReqVO = new BlindMemberLevelExportReqVO();
        memberLevelExportReqVO.setNo(levelNo);
        List<BlindMemberLevelDO> memberLevelList = memberLevelService.getMemberLevelList(memberLevelExportReqVO);
        if (CollectionUtils.isNotEmpty(memberLevelList)){
            memberLevelList.forEach(blindMemberLevelDO -> {
                BlindMembershipSystemVo appMembershipSystemVo = new BlindMembershipSystemVo();
                appMembershipSystemVo.setName(blindMemberLevelDO.getName());
                appMembershipSystemVo.setNo(blindMemberLevelDO.getNo());
                appMembershipSystemVo.setLevelId(blindMemberLevelDO.getId());

                MemberPrivilegeExportReqVO privilegeReqVO = new MemberPrivilegeExportReqVO();
                privilegeReqVO.setLevelId(blindMemberLevelDO.getId());
                //会员特权
                List<MemberPrivilegeDO> memberPrivilegeList = memberPrivilegeService.getMemberPrivilegeList(privilegeReqVO);
                List<Map<String, String>> memberPrivilegeMapList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(memberPrivilegeList)){
                    memberPrivilegeList.forEach(memberPrivilegeDO -> {
                        Map<String, String> memberPrivilegeMap = new HashMap<>();
                        //"text":"打招呼""10次/月"
                        memberPrivilegeMap.put("text",memberPrivilegeDO.getPrivilegeName());
                        memberPrivilegeMap.put("value",memberPrivilegeDO.getPrivilegeNum());
                        memberPrivilegeMapList.add(memberPrivilegeMap);
                    });
                }
                //会员机制
                MemberMechanismExportReqVO mechanismReqVO = new MemberMechanismExportReqVO();
                mechanismReqVO.setLevelId(blindMemberLevelDO.getId());
                List<MemberMechanismDO> memberMechanismList = memberMechanismService.getMemberMechanismList(mechanismReqVO);
                List<Map<String, String>> memberMechanismMapList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(memberMechanismList)){
                    for (MemberMechanismDO memberMechanismDO : memberMechanismList) {
                        Map<String, String> memberMechanismMap = new HashMap<>();
                        String time = memberMechanismDO.getTime();
                        memberMechanismMap.put("text", time);
                        String money = String.valueOf(memberMechanismDO.getMoney());
                        memberMechanismMap.put("value", String.valueOf(memberMechanismDO.getMoney()));
                        //日均
                        int day = Integer.parseInt(time) * 31;
                        int average = Integer.parseInt(money) / day;
                        memberMechanismMap.put("num", String.valueOf(average));
                        memberMechanismMapList.add(memberMechanismMap);
                    }
                }
                appMembershipSystemVo.setPrivilegeList(memberPrivilegeMapList);
                appMembershipSystemVo.setMechanismList(memberMechanismMapList);
                membershipSystemVos.add(appMembershipSystemVo);
            });

        }
        return CommonResult.success(membershipSystemVos);
    }

}
